/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 04/03/2011, 06:04:06
 */

package interfaces;

import java.util.List;

/**
 *
 * @author leandro
 */
public class Principal extends javax.swing.JFrame {

    /** Creates new form Principal */
    public Principal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        janelaLogin = new javax.swing.JPanel();
        tituloPrincipal = new javax.swing.JLabel();
        avisoLogin = new javax.swing.JLabel();
        caixaCPF = new javax.swing.JTextField();
        labelCPF = new javax.swing.JLabel();
        labelSenha = new javax.swing.JLabel();
        caixaSenha = new javax.swing.JPasswordField();
        loginEnviar = new javax.swing.JButton();
        avisoAutenticacao = new javax.swing.JLabel();
        janelaPrincipal = new javax.swing.JPanel();
        campoNomeUser = new javax.swing.JTextField();
        campoMatriculaUser = new javax.swing.JTextField();
        campoCursoUser = new javax.swing.JTextField();
        campoCurriculoUser = new javax.swing.JTextField();
        avisoPrincipal = new javax.swing.JLabel();
        menuPrincipal = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tituloPrincipal.setText("Sistema de Controle de Notas - UFSJ");

        avisoLogin.setBackground(java.awt.Color.white);
        avisoLogin.setFont(new java.awt.Font("Arial Black", 0, 13));
        avisoLogin.setForeground(java.awt.Color.red);
        avisoLogin.setText("Dados Incorretos");
        avisoLogin.hide();

        labelCPF.setText("CPF:");

        labelSenha.setText("Senha:");

        loginEnviar.setText("Enviar");
        loginEnviar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginEnviarActionPerformed(evt);
            }
        });

        avisoAutenticacao.setIcon(new javax.swing.ImageIcon(getClass().getResource("/interfaces/loading.gif"))); // NOI18N
        avisoAutenticacao.setText("Autenticando");
        avisoAutenticacao.setVisible(false);

        javax.swing.GroupLayout janelaLoginLayout = new javax.swing.GroupLayout(janelaLogin);
        janelaLogin.setLayout(janelaLoginLayout);
        janelaLoginLayout.setHorizontalGroup(
            janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, janelaLoginLayout.createSequentialGroup()
                .addGap(88, 88, 88)
                .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, janelaLoginLayout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addComponent(avisoLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                        .addGap(51, 51, 51))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, janelaLoginLayout.createSequentialGroup()
                        .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelSenha)
                            .addComponent(labelCPF))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(caixaSenha)
                            .addComponent(caixaCPF, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(81, 81, 81))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, janelaLoginLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(janelaLoginLayout.createSequentialGroup()
                                .addGap(12, 12, 12)
                                .addComponent(avisoAutenticacao, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(tituloPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(74, 74, 74))
            .addGroup(janelaLoginLayout.createSequentialGroup()
                .addGap(191, 191, 191)
                .addComponent(loginEnviar)
                .addContainerGap(218, Short.MAX_VALUE))
        );
        janelaLoginLayout.setVerticalGroup(
            janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, janelaLoginLayout.createSequentialGroup()
                .addContainerGap(25, Short.MAX_VALUE)
                .addComponent(tituloPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(avisoAutenticacao, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCPF)
                    .addComponent(caixaCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelSenha)
                    .addComponent(caixaSenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(loginEnviar)
                .addGap(12, 12, 12)
                .addComponent(avisoLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        campoNomeUser.setText("nomeUser");

        campoMatriculaUser.setText("numeroUser");

        campoCursoUser.setText("cursoUser");

        campoCurriculoUser.setText("curriculoUser");

        avisoPrincipal.setText("Seja Bem Vindo");

        javax.swing.GroupLayout janelaPrincipalLayout = new javax.swing.GroupLayout(janelaPrincipal);
        janelaPrincipal.setLayout(janelaPrincipalLayout);
        janelaPrincipalLayout.setHorizontalGroup(
            janelaPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(janelaPrincipalLayout.createSequentialGroup()
                .addGroup(janelaPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(janelaPrincipalLayout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(janelaPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(campoMatriculaUser, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoNomeUser, javax.swing.GroupLayout.PREFERRED_SIZE, 249, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoCursoUser, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(campoCurriculoUser, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(janelaPrincipalLayout.createSequentialGroup()
                        .addGap(141, 141, 141)
                        .addComponent(avisoPrincipal)))
                .addContainerGap(152, Short.MAX_VALUE))
        );
        janelaPrincipalLayout.setVerticalGroup(
            janelaPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, janelaPrincipalLayout.createSequentialGroup()
                .addContainerGap(66, Short.MAX_VALUE)
                .addComponent(avisoPrincipal)
                .addGap(36, 36, 36)
                .addComponent(campoNomeUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(campoMatriculaUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(campoCursoUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(campoCurriculoUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(58, 58, 58))
        );

        jMenu1.setText("File");
        menuPrincipal.add(jMenu1);

        jMenu2.setText("Edit");
        menuPrincipal.add(jMenu2);

        setJMenuBar(menuPrincipal);
        menuPrincipal.setVisible(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 460, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(janelaLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(janelaPrincipal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 321, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(janelaLogin, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(janelaPrincipal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    // erro loading / autenticaLogin
    protected void loginEnviarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginEnviarActionPerformed
        //avisoAutenticacao.setVisible(true);        
        String CPF = caixaCPF.getText();
        String PSW = caixaSenha.getText();
        //System.out.println(CPF+" "+PSW);        
        autenticaLogin(CPF,PSW);        
         
    }//GEN-LAST:event_loginEnviarActionPerformed
    protected void autenticaLogin(String CPF, String PSW){
        logica.emulador servidor = new logica.emulador();
        servidor.getServidor("https://www.intranet.ufsj.edu.br");
        if(servidor.logarSCA(CPF, PSW)){
        	// CPF e PSW corretos
        	// muda interface
        	janelaLogin.setVisible(false);
        	menuPrincipal.setVisible(true);
        	// insere dados
        	List<String> listaInfo = servidor.getPrincipalDados();
        	campoMatriculaUser.setText(listaInfo.get(0));
        	campoMatriculaUser.setEditable(false);
        	campoNomeUser.setText(listaInfo.get(1));
        	campoNomeUser.setEditable(false);
        	campoCursoUser.setText(listaInfo.get(2));
        	campoCursoUser.setEditable(false);
        	campoCurriculoUser.setText(listaInfo.get(3));
        	campoCurriculoUser.setEditable(false);
        	janelaLogin.setVisible(false);        	
        } else {
        	// CPF ou/e PSW errado(s)
        	avisoAutenticacao.setVisible(false);
                avisoLogin.show();
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel avisoAutenticacao;
    private javax.swing.JLabel avisoLogin;
    private javax.swing.JLabel avisoPrincipal;
    private javax.swing.JTextField caixaCPF;
    private javax.swing.JPasswordField caixaSenha;
    private javax.swing.JTextField campoCurriculoUser;
    private javax.swing.JTextField campoCursoUser;
    private javax.swing.JTextField campoMatriculaUser;
    private javax.swing.JTextField campoNomeUser;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JPanel janelaLogin;
    private javax.swing.JPanel janelaPrincipal;
    private javax.swing.JLabel labelCPF;
    private javax.swing.JLabel labelSenha;
    private javax.swing.JButton loginEnviar;
    private javax.swing.JMenuBar menuPrincipal;
    private javax.swing.JLabel tituloPrincipal;
    // End of variables declaration//GEN-END:variables

}
